package br.com.sgi.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import org.hibernate.annotations.Type;

import br.com.sgi.util.Constante;
import br.com.sgi.util.ICombobox;

import com.sun.istack.NotNull;

@Entity
@Table(name = "AUTHORITY")
@NamedQueries({

	@NamedQuery(name = "buscarDescricaoPorAuthority", 
			query = "SELECT a.descricao FROM Authority a " +
					" WHERE a.authority like :authority"
					
			)	
			
})
public class Authority  implements ICombobox{

	private static final long serialVersionUID = 1L;

	@Id
	@Column(name="co_seq_authority")
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "seq_authority_seqauthority")  
	@SequenceGenerator(name = "seq_authority_seqauthority", sequenceName = "SGI.seq_authority") 
	private Long coSeqAuthority;
	
	@Size(max=200)
	private String authority;
	
	@Size(max=200)
	private String descricao;
	
	@Column(name="ATIVO", length=1)	
	@Type(type = "br.com.sgi.type.SimNaoType")
	private Boolean ativo;
	
	@NotNull
	@ManyToOne
	@JoinColumn(name="CO_SISTEMA")
	private Sistema sistema;
	
	@Transient
	private String role;
	
	@Transient
	private boolean adminGeral;

	@OneToMany(mappedBy="authority", fetch=FetchType.EAGER)
	private List<Acesso> acessos;
	
	
	public List<Acesso> getAcessos() {
		return acessos;
	}

	public void setAcessos(List<Acesso> acessos) {
		this.acessos = acessos;
	}
	
	public Long getCoSeqAuthority() {
		return coSeqAuthority;
	}

	public void setCoSeqAuthority(Long coSeqAuthority) {
		this.coSeqAuthority = coSeqAuthority;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public String getAuthority() {
		return authority;
	}

	public void setAuthority(String authority) {
		this.authority = authority;
	}

	@Override
	public Object getValue() {
		return getRole();
	}
	
	public String getRole() {
		
		if(getAuthority() != null && getAuthority().equals(Constante.ROLE_ADMIN_GERAL))
			role = "ADMINISTRADOR GERAL";
		else
			if(getAuthority() != null && getAuthority().equals(Constante.ROLE_ADMIN_REGIONAL))
				role = "ADMINISTRADOR REGIONAL";
			else
				if(getAuthority() != null && getAuthority().equals(Constante.ROLE_ADMIN_LOCAL))
					role = "ADMINISTRADOR LOCAL";
				else
					if(getAuthority() != null && getAuthority().equals(Constante.ROLE_USUARIO_LOCAL))
						role = "USUÁRIO LOCAL";
		
		return role;
	}
	

	public void setRole(String role) {
		this.role = role;
	}

	public boolean isAdminGeral() {
		if(coSeqAuthority == null || coSeqAuthority != 1)
			adminGeral = true;
		else
			adminGeral = false;
		return adminGeral;
	}

	public void setAdminGeral(boolean adminGeral) {
		this.adminGeral = adminGeral;
	}

	public Boolean getAtivo() {
		return ativo;
	}

	public void setAtivo(Boolean ativo) {
		this.ativo = ativo;
	}

	public Sistema getSistema() {
		return sistema;
	}

	public void setSistema(Sistema sistema) {
		this.sistema = sistema;
	}
	
	
	
}
